package utils;

import java.util.Comparator;

import uk.ac.nott.cs.g53dia.Cell;
import uk.ac.nott.cs.g53dia.Point;

public class ComparatorFactory {
	public static Comparator<Cell> getCellComparator(final int size) {
		Comparator<Cell> comparator = new Comparator<Cell>() {
			public int compare(Cell o1, Cell o2) {
				int val = o1.getPoint().y / size - o2.getPoint().y / size;
				return val != 0 ? val : o1.getPoint().x / size - o2.getPoint().x / size;
			}
		};
		return comparator;
	}

	public static Comparator<Point> getPointXComparator() {
		return new Comparator<Point>() {
			public int compare(Point o1, Point o2) {
				return o1.x - o2.x;
			}
		};
	}
	
	 public static Comparator<Point> getClosePointComparatorRelativeTo(final Point point) {
	    return new Comparator<Point>() {
	      public int compare(Point o1, Point o2) {
	        return General.distance(point, o1) - General.distance(point, o2); 
	      }
	    };
	  }
	
	public static Comparator<Point> getPointYComparator() {
		return new Comparator<Point>() {
			public int compare(Point o1, Point o2) {
				return o1.y - o2.y;
			}
		};
	}

  public static Comparator<? super Long> getCloseLongComparatorRelativeTo(final long timestep) {
    return new Comparator<Long>() {
      public int compare(Long o1, Long o2) {
        return (int) (Math.abs(timestep - o1.longValue()) - Math.abs(timestep - o2.longValue())); 
      }
    };
  }

}
